package cn.xfdzx.android.apps.shop.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.XFDGoodsDeatailsActivity;
import cn.xfdzx.android.apps.shop.activity.XFDShopDetailsActivity;
import cn.xfdzx.android.apps.shop.activity.remake.ShopRemake_MarketActivity;
import cn.xfdzx.android.apps.shop.adapter.remake.OrderMyRemake_RetailImageListAdapter;
import cn.xfdzx.android.apps.shop.bean.DetailsBean;
import cn.xfdzx.android.apps.shop.bean.MoreGoodsBean;
import cn.xfdzx.android.apps.shop.bean.ShopRemakeListMarketBean;
import cn.xfdzx.android.apps.shop.bean.XFDGoodsDetailsBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.XFDGoodsDetailsPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ImageGlideUtil;
import cn.xfdzx.android.apps.shop.util.ItemOffsetDecoration;
import cn.xfdzx.android.apps.shop.util.SendValidateTimerUtil;
import cn.xfdzx.android.apps.shop.util.StringUtil;
import cn.xfdzx.android.apps.shop.util.TimeUtil;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.video.VideoActivity;
import cn.xfdzx.android.apps.shop.view.SonicView;
import cn.xfdzx.android.apps.shop.view.TagTextView;
import cn.xfdzx.android.apps.shop.widget.CornerImageView;


/**
 * Created by ：gzk
 * time       ：2018/2/5.
 * description:批发商品详情适配器
 */

public class DetailsAdapter extends ListBaseAdapter<DetailsBean> {

    private int layoutID;
    private Context context;
    private boolean is_collenction = false;
    private XFDGoodsDetailsBeen.DataBean data;
    @BindView(R.id.banner_details)
    Banner banner;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.ll_preferential)
    LinearLayout llPreferential;
    @BindView(R.id.ll_service)
    LinearLayout llService;
    @BindView(R.id.ll_evaluation)
    LinearLayout llEvaluation;
    @BindView(R.id.rl_shop)
    RelativeLayout rlShop;
    @BindView(R.id.tv_collection)
    TextView tvCollection;
    @BindView(R.id.tv_shop_address)
    TextView tvShopAddress;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_num)
    TextView tvNum;//2箱起批
    @BindView(R.id.tv_clinch_deal_num)
    TextView tvClinchDealNum;//成交量
    @BindView(R.id.tv_xfdtag)
    TextView tv_xfdtag;
    @BindView(R.id.tv_xfdtagbottom)
    TextView tv_xfdtagbottom;
    @BindView(R.id.tv_goods_name)
    TagTextView tvGoodsName;
    @BindView(R.id.tv_courier)
    TextView tvCourier;
    @BindView(R.id.tv_shop_name)
    TextView tvShopName;
    @BindView(R.id.tv_first_order)
    TextView tvFirstOrder;
    @BindView(R.id.tv_first_order_content)
    TextView tvFirstOrderContent;
    @BindView(R.id.tv_service)
    TextView tvService;
    @BindView(R.id.tv_attr)
    TextView tvAttr;
    @BindView(R.id.ll_specification_selection)
    LinearLayout llSpecificationSelection;
    @BindView(R.id.tv_shop_score)
    TextView tvShopScore;
    @BindView(R.id.iv_shop)
    ImageView ivShop;
    @BindView(R.id.sonic_view)
    SonicView sonicView;
    @BindView(R.id.rl_video_zhibo)
    RelativeLayout rlVideoZhibo;
    @BindView(R.id.ll_limit)
    LinearLayout ll_limit;//限购还剩件数布局
    @BindView(R.id.ll_ms)
    LinearLayout ll_ms;//限时倒计时布局
    @BindView(R.id.tv_H)
    TextView tv_H;//限时倒计时布局，小时
    @BindView(R.id.tv_buyorder)
    TextView tv_buyorder;//一件起订
    @BindView(R.id.img_bg)
    ImageView img_bg;//活动背景
    @BindView(R.id.tv_M)
    TextView tv_M;//限时倒计时布局，分钟
    @BindView(R.id.tvS)
    TextView tv_S;//限时倒计时布局，秒
    @BindView(R.id.tv_oldSaleprice)
    TextView tv_oldSaleprice;//优惠价
    @BindView(R.id.tv_activeprice)
    TextView tv_activeprice;//秒杀现单价
    @BindView(R.id.tv_oldprice)
    TextView tv_oldprice;//秒杀老单价
    @BindView(R.id.tv_specifications)
    TextView tv_specifications;//规格
    @BindView(R.id.tv_activestate)
    TextView tv_activestate;//活动状态
    @BindView(R.id.rl_active)
    RelativeLayout rl_active;//活动布局
    @BindView(R.id.tv_alreaynum)
    TextView tv_alreaynum;//已抢购件数
    @BindView(R.id.img_my)
    CornerImageView img_my;//第一评论头像
    @BindView(R.id.tv_nickname)
    TextView tv_nickname;//第一评论昵称
    @BindView(R.id.tv_remakecontent)
    TextView tv_remakecontent;//第一评论内容
    @BindView(R.id.recycler_img)
    RecyclerView recycler_img;//第一评论图片
    @BindView(R.id.star01)
    ImageView star01;//第一评论星
    @BindView(R.id.star02)
    ImageView star02;//第一评论星
    @BindView(R.id.star03)
    ImageView star03;//第一评论星
    @BindView(R.id.star04)
    ImageView star04;//第一评论星
    @BindView(R.id.star05)
    ImageView star05;//第一评论星
    @BindView(R.id.tv_goodscount)
    TextView tv_goodscount;//第一评论商品数量信息
    @BindView(R.id.ll_firstremake)
    LinearLayout ll_firstremake;//第一评论布局
    @BindView(R.id.tv_aboutweight)
    TextView tv_aboutweight;
    @BindView(R.id.tv_maozhong)
    TextView tv_maozhong;

    private long service_time;
    private long start_time;
    private long end_time;

    private List<MoreGoodsBean.DataBean> moreGoodsList = new ArrayList<>();
    private XfdShopTuiJianAdapter xfdShopTuiJianAdapter;
    private XFDGoodsDetailsPresenter mPresenter;
    private String good_id;
    private ShopRemakeListMarketBean.DataBean.ReplyBean replyBean;

    public DetailsAdapter(Activity context, XFDGoodsDetailsPresenter mPresenter, String good_id) {
        super(context);
        this.context = context;
        this.mPresenter = mPresenter;
        this.good_id = good_id;
    }


    public void onStart() {
        //开始轮播
        if (null != banner)
            banner.startAutoPlay();

    }


    public void onStop() {
        if (null != banner)
            banner.stopAutoPlay();
        if (null != sonicView) {
            sonicView.stop();
        }
    }


    @SuppressLint("ResourceAsColor")
    public void setData(XFDGoodsDetailsBeen.DataBean data) {
        this.data = data;
        //设置图片网址或地址的集合
        banner.setImages(data.getStoreInfo().getSlider_image());
        //必须最后调用的方法，启动轮播图。
        banner.start();
        if (Double.parseDouble(data.getStoreInfo().getDiscount_price()) == 0) {
            tvPrice.setText("¥" + data.getStoreInfo().getPrice() + "/" + data.getStoreInfo().getUnit_name());
            tv_oldSaleprice.setVisibility(View.GONE);
            tv_aboutweight.setText("约¥" + StringUtil.initAboutPrice(data.getStoreInfo().getPrice(), data.getStoreInfo().getGoods_weight()) + "/斤");

        } else {
            if (data.getStoreInfo().getDiscount_price().equals(data.getStoreInfo().getPrice())) {
                tv_oldSaleprice.setVisibility(View.GONE);
            } else {
                tv_oldSaleprice.setVisibility(View.VISIBLE);
            }
            tv_oldSaleprice.setText("¥" + data.getStoreInfo().getPrice());
            tv_oldSaleprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
            tvPrice.setText("¥" + data.getStoreInfo().getDiscount_price() + "/" + data.getStoreInfo().getUnit_name());
            tv_aboutweight.setText("约¥" + StringUtil.initAboutPrice(data.getStoreInfo().getDiscount_price(), data.getStoreInfo().getGoods_weight()) + "/斤");

        }
        tv_maozhong.setText("毛重: " + data.getStoreInfo().getGoods_weight() + "斤");
        //限时限量活动显示
        tv_buyorder.setText(data.getIs_support() + "件起订");
        int activeType = data.getWholesale_activities().getType();
        //  "type": 0,  // 0 没有活动 1元预定活动 2限量活动 3秒杀活动
        if (activeType == 0) {
            ll_ms.setVisibility(View.GONE);
            ll_limit.setVisibility(View.GONE);
            rl_active.setVisibility(View.GONE);
            stopTickWork();
        } else if (activeType == 1) {
            ll_ms.setVisibility(View.GONE);
            ll_limit.setVisibility(View.GONE);
            rl_active.setVisibility(View.GONE);
            stopTickWork();
        } else if (activeType == 2) {
            rl_active.setVisibility(View.VISIBLE);
            ll_ms.setVisibility(View.GONE);
            ll_limit.setVisibility(View.VISIBLE);
            img_bg.setImageResource(R.drawable.ic_goodslimitbg);
            tv_buyorder.setBackgroundResource(R.drawable.d4c27a2_bg_r8);
            tv_activestate.setTextColor(mContext.getResources().getColor(R.color.color_4c27a2));


            if (EmptyUtil.isNotEmpty(data.getWholesale_activities().getLimit_purchase())) {
                tv_activeprice.setText("" + data.getWholesale_activities().getLimit_purchase().getConcessional_price());
                tv_specifications.setText("/" + data.getWholesale_activities().getLimit_purchase().getSpecifications());
                tv_oldprice.setText("¥" + data.getWholesale_activities().getLimit_purchase().getOriginal_price() + "/" +
                        data.getWholesale_activities().getLimit_purchase().getSpecifications());
                tv_alreaynum.setText(data.getWholesale_activities().getLimit_purchase().getStock() + "");
                if (data.getWholesale_activities().getLimit_purchase().getStock() > 0) {
                    tv_activestate.setText("抢购中");
                } else {
                    tv_activestate.setText("已抢光");
                }
                if (Double.parseDouble(data.getWholesale_activities().getLimit_purchase().getConcessional_price()) == 0) {
                    tvPrice.setText("¥" + data.getWholesale_activities().getLimit_purchase().getOriginal_price());
                    tv_oldSaleprice.setVisibility(View.GONE);
                } else {
                    if (data.getWholesale_activities().getLimit_purchase().getConcessional_price().
                            equals(data.getWholesale_activities().getLimit_purchase().getOriginal_price())) {
                        tv_oldSaleprice.setVisibility(View.GONE);
                    } else {
                        tv_oldSaleprice.setVisibility(View.VISIBLE);
                    }
                    tv_oldSaleprice.setText("¥" + data.getWholesale_activities().getLimit_purchase().getOriginal_price());
                    tv_oldSaleprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
                    tvPrice.setText("¥" + data.getWholesale_activities().getLimit_purchase().getConcessional_price());
                }
            } else {
                rl_active.setVisibility(View.GONE);
            }

        } else if (activeType == 3) {
            rl_active.setVisibility(View.VISIBLE);
            ll_ms.setVisibility(View.VISIBLE);
            ll_limit.setVisibility(View.GONE);
            img_bg.setImageResource(R.drawable.ic_msgoodsinfobg);
            tv_buyorder.setBackgroundResource(R.drawable.de22534_bg_r8);
            tv_activestate.setTextColor(mContext.getResources().getColor(R.color.color_e22534));
            if (EmptyUtil.isNotEmpty(data.getWholesale_activities().getLimit_time_good())) {
                tv_activeprice.setText("" + data.getWholesale_activities().getLimit_time_good().getConcessional_price_every_catty());
                tv_specifications.setText("/" + data.getWholesale_activities().getLimit_time_good().getSpecifications());
                tv_oldprice.setText("¥" + data.getWholesale_activities().getLimit_time_good().getOriginal_price_every_catty() + "/" +
                        data.getWholesale_activities().getLimit_time_good().getSpecifications());

                service_time = data.getWholesale_activities().getLimit_time_good().getService_time();
                start_time = data.getWholesale_activities().getLimit_time_good().getStart_time();
                end_time = data.getWholesale_activities().getLimit_time_good().getEnd_time();

                if (service_time >= start_time && service_time < end_time) {
                    mDisableTimeTemp = end_time - service_time;
                    tv_activestate.setText("进行中");
                    stopTickWork();
                    startTickWork();
                } else {
                    mDisableTimeTemp = start_time - service_time;
                    tv_activestate.setText("即将开始");
                    stopTickWork();
                    if (mDisableTimeTemp > 0) {
                        startTickWork();
                    }
                }
                if (Double.parseDouble(data.getWholesale_activities().getLimit_time_good().getConcessional_price()) == 0) {
                    tvPrice.setText("¥" + data.getWholesale_activities().getLimit_time_good().getOriginal_price());
                    tv_oldSaleprice.setVisibility(View.GONE);
                } else {
                    if (data.getWholesale_activities().getLimit_time_good().getConcessional_price().
                            equals(data.getWholesale_activities().getLimit_time_good().getOriginal_price())) {
                        tv_oldSaleprice.setVisibility(View.GONE);
                    } else {
                        tv_oldSaleprice.setVisibility(View.VISIBLE);
                    }
                    tv_oldSaleprice.setText("¥" + data.getWholesale_activities().getLimit_time_good().getOriginal_price());
                    tv_oldSaleprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
                    tvPrice.setText("¥" + data.getWholesale_activities().getLimit_time_good().getConcessional_price());
                }
            } else {
                rl_active.setVisibility(View.GONE);
            }

        }
        tv_oldprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线

        if (data.getShopInfo().getIs_self_store() == 1) {
            tvGoodsName.setSingleTagAndContent("", data.getStoreInfo().getTitle());
        } else {
            tvGoodsName.setSingleTagAndContent("", data.getStoreInfo().getTitle());
        }

        if (0 == data.getStoreInfo().getIs_postage()) {
            tvCourier.setText("不包邮");
        } else {
            tvCourier.setText("包邮");
        }
        Log.e("getDiscount_price", data.getStoreInfo().getDiscount_price() + "111");

        RoundedCorners roundedCorners = new RoundedCorners(20);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
        Glide.with(context).load(data.getShopInfo().getLogo()).apply(options.transform(new CenterCrop(), roundedCorners)).into(ivShop);
        tvShopScore.setText("店铺" + data.getShopInfo().getStar());
        tvShopName.setText(data.getShopInfo().getName());
        //Todo
        tvNum.setText("");
        tvClinchDealNum.setText("成交" + data.getStoreInfo().getSales() + "笔");
        tvFirstOrder.setText(data.getStoreInfo().getPreferential_activities().getTitle());
        tvFirstOrderContent.setText(data.getStoreInfo().getPreferential_activities().getContent());
        tvService.setText(data.getStoreInfo().getGoods_ensures().getTitle());

//        if (data.getProductValue().size() == 0) {
//            tvAttr.setText(data.getStoreInfo().getTitle());
//        }else {
//            tvAttr.setText(data.getProductValue().get(0).getSuk());
//        }
        tvAttr.setText(data.getStoreInfo().getUnit_name());
        llSpecificationSelection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                listener.showAdddPW();
            }
        });
        rlShop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()) {
                    Intent intent = new Intent(mContext, XFDShopDetailsActivity.class);
                    intent.putExtra("shop_id", data.getShopInfo().getId() + "");
                    mContext.startActivity(intent);
                }
            }
        });
        if (EmptyUtil.isNotEmpty(data.getShopInfo().getIs_self_store())) {
            if (data.getShopInfo().getIs_self_store() == 1) {
                tv_xfdtag.setVisibility(View.VISIBLE);
                tv_xfdtagbottom.setVisibility(View.VISIBLE);
            } else {
                tv_xfdtag.setVisibility(View.GONE);
                tv_xfdtagbottom.setVisibility(View.GONE);
            }
        }

        if (data.getLive_id() == 0) {
            rlVideoZhibo.setVisibility(View.GONE);
        } else {
            rlVideoZhibo.setVisibility(View.VISIBLE);
            rlVideoZhibo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, VideoActivity.class);
                    intent.putExtra("live_id", data.getLive_id() + "");
                    intent.putExtra("shop_type", "1");
                    mContext.startActivity(intent);
                }
            });
        }

    }

    public void setRemakeDate(ShopRemakeListMarketBean.DataBean.ReplyBean replyBean) {
        this.replyBean = replyBean;
        ll_firstremake.setVisibility(View.VISIBLE);
        img_my.setType(CornerImageView.TYPE_CIRCLE);
        ImageGlideUtil.loadImage(mContext, img_my, replyBean.getUser().getAvatar());
        if (EmptyUtil.isNotEmpty(replyBean.getPics())) {
            recycler_img.setVisibility(View.VISIBLE);
            recycler_img.setLayoutManager(new GridLayoutManager(mContext, 3));
            OrderMyRemake_RetailImageListAdapter imageListAdapter = new OrderMyRemake_RetailImageListAdapter(replyBean.getPics());
            recycler_img.setAdapter(imageListAdapter);
        } else {
            recycler_img.setVisibility(View.GONE);
        }

        if (EmptyUtil.isNotEmpty(replyBean.getComment())) {
            tv_remakecontent.setVisibility(View.VISIBLE);
            tv_remakecontent.setText(replyBean.getComment());
        } else {
            tv_remakecontent.setVisibility(View.GONE);
        }

        clearState(star01, star02, star03, star04, star05, replyBean.getService_score() + "");
        tv_nickname.setText(replyBean.getUser().getNickname() == null ? "" : replyBean.getUser().getNickname());
        if (EmptyUtil.isNotEmpty(replyBean.getGoods_unit())) {
            tv_goodscount.setText("数量: " + replyBean.getGoods_num() + " 规格: " + replyBean.getGoods_unit()
                    + "(" + replyBean.getGoods_name() + ")");
        } else {
            tv_goodscount.setText("数量: " + replyBean.getGoods_num() + "(" + replyBean.getGoods_name() + ")");
        }
    }

    @Override
    public int getItemViewType(int position) {
        int type = mDataList.get(position).getType();
        if (type == 1) {
//            商品
            layoutID = R.layout.item_good_details_banner;
            return 1001;
        } else if (type == 2) {
//            推荐
            layoutID = R.layout.item_good_details_tuijian;
            return 1002;
        } else if (type == 3) {
//            评价
//            layoutID = R.layout.item_good_details3;
            return 1003;
        } else if (type == 4) {
//            评价
//            layoutID = R.layout.item_good_details4;
            return 1004;
        }
        return super.getItemViewType(position);
    }

    @Override
    public int getLayoutId() {
        return layoutID;
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, int position) {
        int type = getItemViewType(position);
        if (type == 1001) {
            RelativeLayout item = holder.getView(R.id.rl_good_item);
            ButterKnife.bind(this, item);

            ivBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (Utils.isFastClick()) {
                        listener.setFinsh();
                    }
                }
            });
            tvCollection.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (Utils.isFastClick()) {
                        is_collenction = !is_collenction;
                        if (is_collenction) {
                            onCollectionClickListener.setCollectionClickListener("1", data.getStoreInfo().getId());
                        } else {
                            onCollectionClickListener.setCollectionClickListener("2", data.getStoreInfo().getId());
                        }
                    }
                }
            });
            llPreferential.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (Utils.isFastClick()) {
                        listener.setOnPreferential(data.getStoreInfo().getPreferential_activities());
                    }
                }
            });
            llService.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    listener.setOnService();
                }
            });
            llEvaluation.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (EmptyUtil.isNotEmpty(data.getShopInfo())) {
                        if (EmptyUtil.isNotEmpty(replyBean)) {
                            Intent intent = new Intent(mContext, ShopRemake_MarketActivity.class);
                            intent.putExtra("storeId", data.getShopInfo().getId() + "");
                            intent.putExtra("goodsId", good_id);
                            mContext.startActivity(intent);
                        } else {
                            Utils.toastMessage(mContext, "暂无评价");
                        }

                    }

                }
            });

            initBanner();
            getMeasureHeight(item, type);
        }
        if (type == 1002) {
            RelativeLayout item = holder.getView(R.id.rl_good_item);
            RecyclerView rvTuijian = holder.getView(R.id.rv_tuijian);
            TextView tv_lookall = holder.getView(R.id.tv_lookall);
            initRecyclerView(rvTuijian);
            getMeasureHeight(item, type);

            tv_lookall.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Utils.isFastClick()) {
                        Intent intent = new Intent(mContext, XFDShopDetailsActivity.class);
                        if (EmptyUtil.isNotEmpty(data.getShopInfo())) {
                            intent.putExtra("shop_id", data.getShopInfo().getId() + "");
                        } else {
                            Utils.toastMessage(mContext, "未查询到店铺信息");
                            return;
                        }

                        mContext.startActivity(intent);
                    }
                }
            });

        }
        if (type == 1003) {
//            final LinearLayout item = holder.getView(R.id.ll_good_item);
//            RecyclerView mList = holder.getView(R.id.list);
//            mList.setLayoutManager(new LinearLayoutManager(context));
//            mList.setAdapter(mAdapter);
//            mList.setNestedScrollingEnabled(false);
//            getMeasureHeight(item, type);
        }
        if (type == 1004) {
//            WebView webView = holder.getView(R.id.ll_good_item);
//            WebSettings mSetting = webView.getSettings();
//            mSetting.setJavaScriptEnabled(true);
//            mSetting.setBlockNetworkImage(false);//解决图片不显示
//            mSetting.setBuiltInZoomControls(true); // 显示放大缩小
//            mSetting.setSupportZoom(false);
//            url = url.replaceAll("<img", "<img style='width:100%'");
//            webView.loadDataWithBaseURL(null, url, "text/html", "utf-8", null);
//            getMeasureHeight(webView,type);
        }
    }

    /**
     * 设置推荐商品数据
     */
    public void setMoreGoodsList(List<MoreGoodsBean.DataBean> data) {
        moreGoodsList = data;
        xfdShopTuiJianAdapter.setList(moreGoodsList);

    }

    /**
     * 设置收藏显示
     */
    public void setCollectionState(boolean is_collenction) {
        this.is_collenction = is_collenction;
        if (is_collenction) {
            Drawable nav_up = mContext.getResources().getDrawable(R.drawable.icon_collection_red);
            nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
            tvCollection.setCompoundDrawables(null, nav_up, null, null);

        } else {
            Drawable nav_up = mContext.getResources().getDrawable(R.drawable.icon_collection);
            nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
            tvCollection.setCompoundDrawables(null, nav_up, null, null);

        }

    }

    private void initRecyclerView(RecyclerView rvTuijian) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(mContext, R.dimen.item_offset);
        rvTuijian.setLayoutManager(linearLayoutManager);
        rvTuijian.setNestedScrollingEnabled(false);
        rvTuijian.addItemDecoration(itemDecoration);
        xfdShopTuiJianAdapter = new XfdShopTuiJianAdapter(mContext, moreGoodsList);
        xfdShopTuiJianAdapter.setOnItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(context, XFDGoodsDeatailsActivity.class);
                intent.putExtra("good_id", moreGoodsList.get(position).getId() + "");
                context.startActivity(intent);
            }
        });
        rvTuijian.setAdapter(xfdShopTuiJianAdapter);
    }


    private void initBanner() {
        banner.setImageLoader(new GlideImageLoader());
        banner.setBannerStyle(BannerConfig.NUM_INDICATOR);
        banner.setIndicatorGravity(BannerConfig.RIGHT);
        banner.setBannerAnimation(Transformer.Default);
        banner.isAutoPlay(true);
        banner.setDelayTime(3000);
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int i) {

            }
        });

    }


    private class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            RequestOptions options = new RequestOptions();
            options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                    .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                    .error(R.drawable.icon_defult);
            Glide.with(context).load(path).apply(options).into(imageView);
        }
    }

    /**
     * 获取每个item的高度
     *
     * @param view item的跟布局
     * @param type 用于判断是那个item的高度
     */
    public void getMeasureHeight(final View view, final int type) {
        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (listener != null) {
//                    if(type==1003){
//                        if(height!=0){
//                            height+=view.getHeight();
//                            listener.setOnItemHeightListener(height,type);
//                        }else{
//                            height=view.getHeight();
//                        }
//                    }else{
                    listener.setOnItemHeightListener(view.getHeight(), type);
//                    }

                }
            }
        });
    }

    public interface OnItemHeightListener {
        void setOnItemHeightListener(int height, int type);

        void setOnPreferential(XFDGoodsDetailsBeen.DataBean.StoreInfoBean.PreferentialActivitiesBean bean);

        void setOnService();

        void setFinsh();

        void showAdddPW();
    }

    private OnItemHeightListener listener;

    public void setListener(OnItemHeightListener listener) {
        this.listener = listener;
    }

    public interface OnCollectionClickListener {
        void setCollectionClickListener(String type, int goodsId);

    }

    public void setCollectionListener(OnCollectionClickListener listener) {
        this.onCollectionClickListener = listener;
    }

    private OnCollectionClickListener onCollectionClickListener;

    //活动开始前倒计时
    private SendValidateTimerUtil mTimer = new SendValidateTimerUtil();
    private long mDisableTimeTemp = 0;

    /**
     * 开启倒计时
     */
    public void startTickWork() {

        mTimer.startWork(0, 1000, new SendValidateTimerUtil.SDTimerListener() {
            @Override
            public void onWorkMain() {
                if (service_time >= start_time && service_time < end_time) {
                    tickWork();
                } else {
                    tickWorkFront();
                }

            }

            @Override
            public void onWork() {
            }
        });
        // }
    }

    /**
     * 每秒钟调用一次
     */
    private void tickWork() {
        mDisableTimeTemp--;
        if (mDisableTimeTemp >= 0) {
            //执行
            TimeUtil.setFormattedTime(mDisableTimeTemp, tv_H, tv_M, tv_S);
        } else {
            mPresenter.initData(good_id);
        }
    }

    /**
     * 活动开始前
     * 每秒钟调用一次
     */
    private void tickWorkFront() {
        mDisableTimeTemp--;
        Log.e("service_time", mDisableTimeTemp + "===");
        if (mDisableTimeTemp >= 0) {
            //执行
        } else {//活动开始请求接口
            mPresenter.initData(good_id);
        }
    }

    public void stopTickWork() {
        mTimer.stopWork();
    }

    private void clearState(ImageView star01, ImageView star02, ImageView star03, ImageView star04, ImageView star05, String starnum) {
        star01.setVisibility(View.GONE);
        star02.setVisibility(View.GONE);
        star03.setVisibility(View.GONE);
        star04.setVisibility(View.GONE);
        star05.setVisibility(View.GONE);

        switch (starnum) {
            case "1":
                star01.setVisibility(View.VISIBLE);
                break;
            case "2":
                star01.setVisibility(View.VISIBLE);
                star02.setVisibility(View.VISIBLE);
                break;
            case "3":
                star01.setVisibility(View.VISIBLE);
                star02.setVisibility(View.VISIBLE);
                star03.setVisibility(View.VISIBLE);
                break;
            case "4":
                star01.setVisibility(View.VISIBLE);
                star02.setVisibility(View.VISIBLE);
                star03.setVisibility(View.VISIBLE);
                star04.setVisibility(View.VISIBLE);
                break;
            case "5":
                star01.setVisibility(View.VISIBLE);
                star02.setVisibility(View.VISIBLE);
                star03.setVisibility(View.VISIBLE);
                star04.setVisibility(View.VISIBLE);
                star05.setVisibility(View.VISIBLE);
                break;
        }
    }

}
